﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using LightWeb.Respository;
using LightWeb.DomainModel;

namespace LightWeb.Blog.Admin
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "AdminService" in code, svc and config file together.
    public class AdminService : IAdminService
    {
        public string ReCreateDbSchema()
        {
            try
            {
                var _cfg = new Configuration();
                _cfg.Configure();
                SchemaExport export = new SchemaExport(_cfg);
                export.Execute(true, true, false);
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }


        public string InitDbData()
        {
            try
            {
                using (OurTransactionScope ts = new OurTransactionScope())
                {
                    var context = DataContext.Current;
                    for (int i = 0; i < 100; i++)
                    {
                        var articalId = context.Session.Save(new ArticalInfo()
                           {
                               Title = "LightWeb Blog #" + i + " Artical",
                               Summary = "Artical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical "
                               + "SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical "
                               + "SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical SummaryArtical "
                               + "SummaryArtical SummaryArtical Summary",
                               PostDate = DateTime.Now,
                               UpdateDate = DateTime.Now
                           });
                        for (int j = 0; j < 5; j++)
                        {
                            context.Session.Save(new ArticalComment()
                            {
                                ArticalId = i,
                                Content = "Comment Content",
                                Date = DateTime.Now,
                                Email = "flyear.cheng@gmail.com",
                                Name = "Flyear",
                                Site = "http://flyear.cnblogs.com"
                            });
                        }
                    }

                    ts.Complete();
                }
                return "success";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }


        }
    }
}
